<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>HTTP协议概要 | jiaojiao&#39;s blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/jiaojiao-blog/狗狗.png">
    <meta name="description" content="jiaojiao">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/jiaojiao-blog/assets/css/0.styles.eb0c6272.css" as="style"><link rel="preload" href="/jiaojiao-blog/assets/js/app.9b9a9edd.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/3.13686b80.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/1.7e378398.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/9.6322dd00.js" as="script"><link rel="prefetch" href="/jiaojiao-blog/assets/js/10.3a421832.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/11.c69afd6f.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/12.5a57ed37.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/13.9bec8c42.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/14.506f6e6c.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/15.d294a059.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/16.6c9ad749.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/17.fed338db.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/18.7c6cff98.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/19.1b119cf7.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/20.b4e422a5.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/21.d3188793.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/22.f0f65d80.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/23.a86a53ea.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/24.b86aa33d.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/4.cd307e36.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/5.336e8cbd.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/6.8f2d8ec2.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/7.952972ee.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/8.7270e456.js">
    <link rel="stylesheet" href="/jiaojiao-blog/assets/css/0.styles.eb0c6272.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-5bb33761><div data-v-5bb33761><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-5bb33761 data-v-5bb33761><h3 class="title" data-v-59e6cb88>jiaojiao's blog</h3> <p class="description" data-v-59e6cb88>jiaojiao</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>jiaojiao</span>
          
        <!---->
        2022
      </a></span></div></div> <div class="hide" data-v-5bb33761><header class="navbar" data-v-5bb33761><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/jiaojiao-blog/" class="home-link router-link-active"><img src="/jiaojiao-blog/香蕉.png" alt="jiaojiao's blog" class="logo"> <span class="site-name">jiaojiao's blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/jiaojiao-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/cs_study/" class="nav-link"><i class="undefined"></i>
  cs_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/js_study/" class="nav-link"><i class="undefined"></i>
  js_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/ts_study/" class="nav-link"><i class="undefined"></i>
  ts_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/vue_study/" class="nav-link"><i class="undefined"></i>
  vue_study
</a></li></ul></div></div><div class="nav-item"><a href="/jiaojiao-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/jiaojiao-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/读书笔记/" class="nav-link"><i class="undefined"></i>
  读书笔记
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/life/" class="nav-link"><i class="undefined"></i>
  生活
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/webDiary/" class="nav-link"><i class="undefined"></i>
  前端日记
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/li-jiao-is-scorched-green" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-5bb33761></div> <aside class="sidebar" data-v-5bb33761><div class="personal-info-wrapper" data-v-1fad0c41 data-v-5bb33761><img src="/jiaojiao-blog/头像.jpg" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    jiaojiao
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>14</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>5</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/jiaojiao-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/cs_study/" class="nav-link"><i class="undefined"></i>
  cs_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/js_study/" class="nav-link"><i class="undefined"></i>
  js_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/ts_study/" class="nav-link"><i class="undefined"></i>
  ts_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/vue_study/" class="nav-link"><i class="undefined"></i>
  vue_study
</a></li></ul></div></div><div class="nav-item"><a href="/jiaojiao-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/jiaojiao-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/读书笔记/" class="nav-link"><i class="undefined"></i>
  读书笔记
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/life/" class="nav-link"><i class="undefined"></i>
  生活
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/webDiary/" class="nav-link"><i class="undefined"></i>
  前端日记
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/li-jiao-is-scorched-green" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-5bb33761><h3 class="title" data-v-59e6cb88>HTTP协议概要</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>jiaojiao</span>
          
        <!---->
        2022
      </a></span></div></div> <div data-v-5bb33761><div data-v-5bb33761><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">HTTP协议概要</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>jiaojiao</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>8/18/2022</span></i> <i class="iconfont reco-eye" data-v-8a445198><span id="/jiaojiao-blog/blogs/cs_study/2022/HTTP%E5%8D%8F%E8%AE%AE.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-8a445198><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>网络</span></i></div></div> <div class="theme-reco-content content__default"><h5 id="http协议与web"><a href="#http协议与web" class="header-anchor">#</a> HTTP协议与WEB</h5> <p>HTTP协议</p> <ul><li>超文本传输协议(http)是一种通信协议，它允许将超文本标记语言(html)文档从Web服务器传送到客户端的浏览器</li> <li>HTTP是一个属于应用层的面向对象的协议，由于其简介、快速的方式，适用于分布式超媒体信息系统。它与1990年提出，经过几年的使用与发展，得到不断地完善和扩展</li></ul> <p>WEB</p> <ul><li>WEB是一种基于超文本和HTTP的、全球性的、动态交互的、跨平台的分布式图形信息系统</li> <li>建立在Internet上的一种网络服务、为浏览者在Internet上查找和浏览信息提供图形化的、易于访问的直观界面、其中的文档及超级链接将Internet上的信息节点组织成一个互为关联的网状结构</li></ul> <h5 id="传输层-tcp三次握手"><a href="#传输层-tcp三次握手" class="header-anchor">#</a> 传输层-TCP三次握手</h5> <p>位码即TCP标志位，有6种标示：</p> <ul><li>SYN(synchronous建立联机)</li> <li>ACK(acknowledgement 确认)</li> <li>PSH(push传送)</li> <li>FIN(finish结束)</li> <li>RST(reset重置)</li> <li>URG(urgent紧急)</li> <li>Sequence number(顺序号码) Acknowledge number(确认号码)</li></ul> <blockquote><p>其中，ACK是可能与SYN，FIN等同时使用的，比如SYN和ACK可能同时为1，它表示的就是建立连接之后的响应，</p> <p>如果只是单个的一个SYN，它表示的只是建立连接。</p> <p>TCP的几次握手就是通过这样的ACK表现出来的。</p> <p>但SYN与FIN是不会同时为1的，因为前者表示的是建立连接，而后者表示的是断开连接。</p> <p>RST一般是在FIN之后才会出现为1的情况，表示的是连接重置。</p> <p>一般地，当出现FIN包或RST包时，我们便认为客户端与服务器端断开了连接；而当出现SYN和SYN＋ACK包时，我们认为客户端与服务器建立了一个连接。</p> <p>PSH为1的情况，一般只出现在 DATA内容不为0的包中，也就是说PSH为1表示的是有真正的TCP数据包内容被传递。</p></blockquote> <p><img src="/jiaojiao-blog/assets/img/uTools_1660800585799.c6587ebf.png" alt=""></p> <p>第一次握手：主机A发送位码为syn＝1，随机产生seq number=1234567的数据包到服务器，主机B由SYN=1知道，A要求建立联机；</p> <p>服务端能确认</p> <p><strong>服务端收与客户端发正常</strong></p> <p><img src="/jiaojiao-blog/assets/img/uTools_1660800668561.babe9dd6.png" alt=""></p> <p>第二次握手：主机B收到请求后要确认联机信息，向A发送ack number=(主机A的seq+1)，syn=1，ack=1，随机产生seq=7654321的包；</p> <p>客户端确认</p> <p><strong>客户端收发正常</strong></p> <p><strong>服务端收发正常</strong></p> <p><img src="/jiaojiao-blog/assets/img/uTools_1660801657949.dd9c5725.png" alt=""></p> <p>第三次握手：主机A收到后检查ack number是否正确，即第一次发送的seq number+1，以及位码ack是否为1，若正确，主机A会再发送ack number=(主机B的seq+1)，ack=1，主机B收到后确认seq值与ack=1则连接建立成功。</p> <p>服务端确认</p> <p><strong>服务端发正常与客户端收正常</strong></p> <p>DNS服务：相对于IP地址(纯数字)，域名更容易让人记住。但TCP/IP协议是使用IP地址进行访问的，所以必须有个机制或服务能让域名转换成IP地址。DNS服务就是用来解决这个问题的，它提供域名到IP地址之间的解析服务</p> <h5 id="http协议特点"><a href="#http协议特点" class="header-anchor">#</a> HTTP协议特点</h5> <p>支持客户/服务器模式</p> <ul><li>客户/服务器模式工作的方式是有客户端向服务器发出请求、服务端响应请求、并进行相应的服务</li></ul> <p>简单快速</p> <ul><li>客户向服务器请求时、只需传送请求方法和路径</li> <li>请求方法常用的有GET、HEAD、POST。每种方法规定了客户与服务器联系的类型不同</li> <li>由于HTTP协议简单、使得HTTP服务器的程序规模小、因而通信速度很快</li></ul> <p>灵活</p> <ul><li>HTTP允许传输任意类型的数据对象</li> <li>正在传输的类型有Content-Type标识</li></ul> <p>无连接(面向连接)</p> <ul><li>无连接的含义是限制每次连接只处理一个请求</li> <li>服务器处理完客户的请求、并收到客户的应答后、即断开连接</li> <li>采用这种方式可以节省传输时间、节约网络资源</li></ul> <p>无状态</p> <ul><li>HTTP协议是无状态协议</li> <li>无状态是指协议对于事务处理没有记忆能力。缺少状态意味着每次后续处理需要前面的信息，则它必须重传，这样可能导致每次连接传送的数据量增大</li> <li>另一方面，在服务器不需要先前信息时它的应答就较快</li></ul> <h5 id="web-socket"><a href="#web-socket" class="header-anchor">#</a> web socket</h5> <p>web socket是HTML5新增的协议</p> <p>目的：在浏览器与服务器之间建立一个不受限的双向通信的通道</p> <p>以前</p> <p>客户端===请求=》服务器</p> <p>服务器===响应=》客户端</p> <h5 id="为什么传统的http协议不能做到websocket实现的功能"><a href="#为什么传统的http协议不能做到websocket实现的功能" class="header-anchor">#</a> 为什么传统的http协议不能做到websocket实现的功能？</h5> <p>因为http协议是一个==请求-响应==协议，请求必须先由浏览器发送给服务器，服务器才能响应</p> <p>实时聊天、在线炒股（没有websocket只能使用轮询 ）</p> <p>轮询的缺点：实时性不够，频繁请求会给服务器带来很大的压力</p> <p>websocket并不是全新的协议，是利用了http协议来建立的</p> <p>node是基于COMMONJS进行包的导入</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> Websocket <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'ws'</span><span class="token punctuation">)</span>
<span class="token comment">//引入Server类并且实例化，定义服务器端口</span>
<span class="token keyword">const</span> wss <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Websocket<span class="token punctuation">.</span>Server</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">port</span><span class="token operator">:</span><span class="token number">5000</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

wss<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'connection'</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ws</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  ws<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'message'</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">message</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'客户端发送的信息时'</span><span class="token operator">+</span>message<span class="token punctuation">)</span>
    ws<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'hi，我接收到了你的信息，我是服务器'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">let</span> ws <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Websocket</span><span class="token punctuation">(</span><span class="token string">'ws://localhost:5000'</span><span class="token punctuation">)</span>
<span class="token comment">//连接成功后</span>
ws<span class="token punctuation">.</span><span class="token function-variable function">onopen</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">evt</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'连接成功'</span><span class="token punctuation">)</span>
  ws<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'hello 我是客户端'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">//传递和接受服务器数据的方法</span>
ws<span class="token punctuation">.</span><span class="token function-variable function">onmessage</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">evt</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  ws<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'我是客户端发的第二条信息'</span><span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'服务器传过来的信息时：'</span><span class="token operator">+</span>evt<span class="token punctuation">.</span>dat<span class="token punctuation">)</span>
  ws<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h5 id="http缓存策略详解"><a href="#http缓存策略详解" class="header-anchor">#</a> HTTP缓存策略详解：</h5> <p>浏览器每次发起请求，先在本地缓存中查找结果以及缓存标识，根据缓存标识判断是否使用本地缓存，如果缓存有效，使用本地缓存；否则向服务器发起请求并携带缓存标识，<strong>根据是否需要向服务器发起http请求，将缓存分为两个部分</strong></p> <h5 id="_1-强缓存"><a href="#_1-强缓存" class="header-anchor">#</a> 1，强缓存</h5> <p>浏览器第一次请求时，服务器返回资源，浏览器缓存在本地，并且服务器会通知浏览器一个缓存事件，下次请求如果在缓存时间内，直接使用缓存，否则进入协商缓存</p> <p>控制强缓存的字段有</p> <blockquote><p>Expires(http1.0)</p> <p>Cache-control(http1.1)</p></blockquote> <ul><li>Expires是一个绝对事件，表示这个时间点之前发起的请求都可以从缓存中读取数据；</li> <li>Cache-control是一个相对事件，表示自上次请求多少秒时间内资源有效</li></ul> <blockquote><p>Cache-control的优先级高于Expires,Cache-control解决了Expires因为浏览器事件被手动修改导致缓存判断错误的问题。</p> <p>Cache-control常用字段值：</p> <ul><li>Max-age:最大有效时间；</li> <li>no-cache:不使用强缓存，需要与服务器验证缓存是否过期</li> <li>no-store:本地和代理服务器都不可以使用缓存</li> <li>public:所有内容都可以被缓存</li> <li>private:所有内容只有客户端可以缓存，代理服务器不能缓存</li></ul></blockquote> <p>2，协商缓存</p> <p>用于判断缓存是否有用，主要包含两个关键字段：Last-Modified和E-tag.</p> <p>Last-Modified表示资源最后修改时间，下一次请求相同资源时，浏览器将Last-Modified的值写入到If-Modified-Since字段，服务器将If-Modified-Since的值和Last-Modified字段进行对比，如果相等，则表示未修改，返回304状态码，反之返回200状态码和数据</p> <p>因此会出现问题：只要资源修改，无论内容是否发生实质变化，都会将该资源返回客户端，并且无法识别1s内进行多次修改的情况，如果资源更新的速度是秒以下的单位，该缓存是不能使用的，因为他的单位也是秒。为了解决这个问题，使用Etag配合If-None-Match</p> <p>E-tag存储的是文件的特殊标识（其实就是文件的hash值），其他过程与上面一样，E-tag的优先级要高于LaSt-Modified</p></div></section> <footer class="page-edit"><!----> <!----></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:0;" data-v-ac050c62></ul></main></div> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/jiaojiao-blog/assets/js/app.9b9a9edd.js" defer></script><script src="/jiaojiao-blog/assets/js/3.13686b80.js" defer></script><script src="/jiaojiao-blog/assets/js/1.7e378398.js" defer></script><script src="/jiaojiao-blog/assets/js/9.6322dd00.js" defer></script>
  </body>
</html>
